package com.itheima.health.mobile.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.mobile.vo.OrderSubmitParam;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("mobile/order")
@Slf4j
public class OrderController {
    @DubboReference
    private OrderService orderService;
    @DubboReference
    private SetmealService setmealService;
    @DubboReference
    private MemberService memberService;
    @DubboReference
    private SmsService smsService;

    /**
     * @Author: 位帅帅
     * @Date: 19:16 2021/1/10
     * @Params: [param]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 提交预约信息
    */
    @PostMapping("submit")
    public Result submit(@RequestBody OrderSubmitParam param) {
        log.info("[预约-提交]param: {}",param);
        //验证码验证
        boolean flag = smsService.checkValidateCode("ORDER",param.getTelephone(),param.getValidateCode());
        if (!flag) {
            log.info("[提交预约-验证验证码]flag: {}",flag);
            return new Result(false,MessageConst.VALIDATECODE_ERROR);
        }
        //创建会员
        Member member = new Member();
        member.setIdCard(param.getIdCard());
        member.setPhoneNumber(param.getTelephone());
        member.setName(param.getName());
        member.setSex(param.getSex());
        member = memberService.addOrUpdate(member);
        //提交体检信息
        Order order = new Order();
        order.setOrderStatus(order.ORDERSTATUS_NO);
        order.setSetmealId(param.getSetMealId());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderDate(param.getOrderDate());
        order.setMemberId(member.getId());
        order = orderService.add(order);
        log.info("[预约-提交]order: {}",order);
        return new Result(true, MessageConst.ORDER_SUCCESS,order);
    }

    /**
     * @Author: 位帅帅
     * @Date: 20:12 2021/1/10
     * @Params: [id]
     * @RerurnType: com.itheima.health.entity.Result
     * @Description: 根据id查询预约信息
    */
    @GetMapping("findById")
    public Result findById(Integer id) {
        log.info("[预约-id查询]id: {}",id);
        Order order = orderService.findById(id);
        Member member = memberService.findById(order.getMemberId());
        Setmeal setmeal = setmealService.findById(order.getSetmealId());
        //封装返回结果
        Map<String,Object> map = new HashMap<>();
        map.put("member",member.getName());
        map.put("setmeal",setmeal.getName());
        map.put("orderDate",new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate()));
        map.put("orderType",order.getOrderType());
        log.info("[预约-id查询]map: {}",map);
        return new Result(true,MessageConst.QUERY_ORDER_SUCCESS,map);
    }
}
